from openai import OpenAI

data = { 
"chushi_checkpoint": [ 
{ 
"checkpoint": "询问客户姓氏", 
"checkpoint_describe": "询问客户的姓名" 
}, 
{ 
"checkpoint": "询问进店意图", 
"checkpoint_describe": "询问客户来店的目的" 
} 
],
"input":"您好，请问您贵姓,您的进店原因是？"
}


client = OpenAI(
    api_key="sk-fc0b61726c1d45b2a11946e8caa5ec62",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)

def predbailian(prompt):
    for i in range(10):
        try:
            completion = client.chat.completions.create(
                model="qwen-plus",
                messages=[
                    {'role': 'system', 'content': 'You are a helpful assistant.'},
                    {'role': 'user', 'content': prompt}],
                temperature=0.001
            )
            content = completion.choices[0].message.content
            return content
        except:
            print(11111)
class LLM_GET:
    def __init__(self) -> None:
        pass
    def get_label(self, data):
        label_desc = list()
        label_name = list()
        for i in data['chushi_checkpoint']:
            label_desc.append(i['checkpoint']+':'+i['checkpoint_describe'])
            label_name.append(i['checkpoint'])
            
        label_desc_content = '\n'.join(label_desc)
        label_name_content = '\n'.join(label_name)

        question = data['input']
        prompt = f"""
                你是一个标签机器人，你的作用是将用户说的话匹配到对应的标签。注意:
                一句话可能不止一个标签,也可能没有标签,
                只需要输出对应标签名，其余不需要输出。
                如果有多个标签请用--来分割。
                如果没有标签，请输出无标签。
                
                标签定义:
                
                {label_desc_content}
                
                标签:
                
                {label_name_content}
                
                用户的话:
                {question}
                    """   
        print('prompt:',prompt)
        res = predbailian(prompt)
        print('xxxx',res)
        res_list = res.split('--')
        res_dict = {'label_name':res_list}
        return res_dict
# LG = LLM_GET()
# print(LG.get_label(data))